package com.situ.mall.controller.back;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.Product;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.JSONResult;
import com.situ.mall.util.LayUITableResult;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private IProductService productService;
	@Autowired 
	private ICategoryService categoryService;
	
	@RequestMapping("/selectByParentId")
	@ResponseBody
	public List<Category> selectByParentId() {
		System.out.println("enclosing_type.enclosing_method()");
		
		return productService.selectByParentId();
	}
	
	@RequestMapping("/selectByCategory")
	public String selectByCategory(Integer parentId,Model model) {
		System.out.println("ProductController.selectByCategory()");
		List<Category> list = productService.selectByCategory(parentId);
		model.addAttribute("list", list);
		
		return "/category2.jsp";
	}
	
	@RequestMapping("/selectByCategoryId")
	public String selectByCategoryId(Integer categoryId,Model model) {
		System.out.println("ProductController.selectByCategoryId()");
		List<Product> list = productService.selectByCategoryId(categoryId);
		model.addAttribute("list", list);
		for (Product product : list) {
			System.out.println(product);
		}
		
		return "/product.jsp";
	}
	
	@RequestMapping("/selectByPage")
	@ResponseBody
	public LayUITableResult selectByPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit) {
		System.out.println("ProductController.selectByPage()");
		
		return productService.selectByPage(page,limit);
	}
	
	@RequestMapping("/getProductInsertPage")
	public String getProductInsertPage(Model model) {
		System.out.println("ProductController.getProductInsertPage()");
		List<Category> list = productService.selectTopCategoryList();
		model.addAttribute("topCategoryList", list);
		for (Category category : list) {
			System.out.println(category);
		}
		
		return "/jsp/back/product_insert.jsp";
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public JSONResult insert(Product product) {
		System.out.println("ProductController.enclosing_method()");
		
		return productService.insert(product);
	}
}
